<?
$PageTitle=$msg["Page_Plugins"];
$toppage = "find";

include "div_menu.php";

db_init_tags();

$aargs = explode("&", $args);
$n = count($aargs);
for ($i = 0; $i < $n; $i++) 
{
  $si = $aargs[$i];
  $s1 = strtok($si, "=");
  $s2 = urldecode(strtok("="));
  if ($s2)
  {
    $request[$s1] = ($request[$s1]) ? $request[$s1]." ".$s2 :$s2;
  }
}

try 
{
  db_call();
  $all_plugs = db_find_plugins($request);

  print "<div class=\"plugins\">\n";
  print "<div class=\"list_in\">\n";

  $n = count($all_plugs);
  if ($n == 0)
  {
    print "{$msg["NoPlugins"]}<br>\n";
    print "<a href=\"find\">{$msg["Continue"]}</a>\n";
  }
  else
  {
    for ($i = 0; $i < $n; $i++) 
    {
      if ($i <> 0)
      { 
        print "<div class=\"list_space\"></div>\n";
      }

      print "<div class=\"topic\">\n";
      print "<div class=\"topic_in\">\n";

      print "<div class=\"topic_in2\">\n";
      $plug = $all_plugs[$i];
      print_plug_wide($plug, "plugin?id=".$plug["id"]);
      print "</div>\n";
  
      print "</div>\n";
      print "</div>\n";
    }
  }

  print "</div>\n";
  print "</div> <!--plugins-->\n";
} 
catch (Exception $e) 
{
  print_errors(array($e->getMessage()));
}


//-----------------------------------------------------------------------------

function db_find_plugins($request) 
{
  if ($request["category"])
  {
    $where = "category='".mysql_escape_string($request["category"])."'";
  }


  if ($request["name"])
  {
    $where = sql_and($where, "(name LIKE '%{$request["name"]}%') OR (description LIKE '%{$request["name"]}%')");
  }


  if ($request["author"])
  {
    $where = sql_and($where, "author LIKE '%{$request["author"]}%'");
  }


  if ($request["tag"])
  {
    $tags = explode(" ", $request["tag"]);
    $n = count($tags);
    for ($i = 0; $i < $n; $i++) 
    {
      $where = sql_and($where, "tags LIKE '%|{$tags[$i]}|%'");
    }
  }


  if ($request["updated"])
  {
    $ts = time();
    $spd = 24 * 60 * 60;
    switch ($request["updated"])
    {
      case 1:  //PastDay
        $ts -= $spd;
        break;
      case 2:  //PastWeek
        $ts -= $spd * 7;
        break;
      case 3:  //PastMonth
        $ts -= $spd * 30;
        break;
      case 4:  //PastYear
        $ts -= $spd * 365;
        break;
    }
    $dt = timestamp_to_sql_datetime($ts);
    $where = sql_and($where, "update_date >= \"{$dt}\"");
  }


  if (!$where)    
  {
    $where = "1=1";
  }


  switch ($request["sortby"])
  {
    case 1:
      $sort = "name asc";
      break;
    case 2:
      $sort = "update_date desc, name asc";
      break;
    case 3:
      $sort = "rating desc, name asc";
      break;
    default:
      $sort = "name asc";
  }


  $sql = sprintf("SELECT * FROM plugins WHERE %s ORDER BY %s", $where, $sort );

//print $sql."<br>";

  return db_read_recs( $sql );
};


function sql_and($expr1, $expr2)
{
  if ($expr1 && $expr2)
  {
    return "($expr1) AND ($expr2)";
  }
  else
  {
    if ($expr1)
    {
      return $expr1;
    }
    else
    {
      return $expr2;
    }
  }
}


?>
